package com.zts.modules.sys.service.impl;

/**
 * Created by jisd on 2017/6/16.
 */

import com.zts.base.exception.InternalServerErrorException;
import com.zts.base.exception.InvalidArgumentException;
import com.zts.base.exception.SourceNotFoundException;
import com.zts.base.page.PageBound;
import com.zts.modules.sys.entity.Resource;
import com.zts.modules.sys.entity.Role;
import com.zts.modules.sys.mapper.ResourceMapper;
import com.zts.modules.sys.mapper.RoleMapper;
import com.zts.modules.sys.service.ResourceService;
import com.zts.modules.sys.service.RoleService;
import com.zts.util.BeanUtil;
import com.zts.util.BeanValidatorsUtil;

import java.util.*;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    ResourceMapper resourceMapper;

    @Autowired
    RoleService roleService;


    public Resource get(Long id) {
        if (null != id && id >= 0) {
            return resourceMapper.get(id);
        } else {
            throw new InvalidArgumentException("参数<id>为空");
        }
    }

    @Override
    public List<Resource> getByEntity(Resource entity) {
        return null;
    }

    @Override
    public List<Resource> getByCondition(Map<String, Object> conditions) {
        return null;
    }


    public List<Resource> getList(Resource entity, PageBound pageBound) {
        if (null == entity) {
            entity = new Resource();
        }

        return resourceMapper.getList(entity, pageBound);
    }

    @Transactional
    public int update(Resource resource) {
        if (null == resource || null == resource.getId() || resource.getId() <= 0) {
            throw new InvalidArgumentException("参数对象id为空");
        }
        Resource old = resourceMapper.get(resource.getId());
        if (null == old) {
            throw new SourceNotFoundException("id[" + resource.getId() + "]对应的资源不存在！");
        }
        BeanUtils.copyProperties(resource, old, BeanUtil.getNullPropertyNames(resource));
        BeanValidatorsUtil.validateWithException(old);
        return resourceMapper.update(old);
    }

    @Transactional
    public int delete(Resource resource) {
        if (null == resource) {
            throw new InvalidArgumentException("对象不能为空");
        } else if (null == resource.getId() || resource.getId() <= 0) {
            throw new InvalidArgumentException("必须传入id ");
        }
        Resource old = resourceMapper.get(resource.getId());
        if (null == old) {
            throw new SourceNotFoundException("id[" + resource.getId() + "]对应的资源不存在！");
        }
        return resourceMapper.delete(resource);
    }

    public Resource save(Resource entity) {
        return null;
    }

    @Transactional
    public Resource insert(Resource entity) {
        if (null == entity) {
            throw new InvalidArgumentException("输出的参数不合法");
        } else {
            BeanValidatorsUtil.validateWithException(entity);
            int i = resourceMapper.insert(entity);
            if (i <= 0 || entity.getId() == null) {
                throw new InternalServerErrorException("新增失败");
            }
            return resourceMapper.get(entity.getId());
        }
    }

    @Override
    public List<Resource> getByRoles(List<Long> roleIds, String type, String resourceCode) {
        if (roleIds.isEmpty()) {
            throw new InvalidArgumentException("用户没有角色");
        }

        String ids = StringUtils.join(roleIds.toArray(), ',');
        return resourceMapper.getByRoles(ids, type, resourceCode);
    }

    @Override
    public List<Resource> getByRoles(Long roleId, String type) {
        List<Long> ids = new ArrayList<>();
        ids.add(roleId);
        return getByRoles(ids, type, null);
    }

    @Transactional
    @Override
    public int addRoleResource(Long roleId, List<Long> resourceIds) {
        checkParas(roleId, resourceIds);
        if (null == resourceIds || resourceIds.isEmpty()) {
            throw new InvalidArgumentException("resourceIds不能为空");
        }
        return resourceMapper.addRoleResource(roleId, resourceIds);

    }

    @Transactional
    @Override
    public int deleteRoleResource(Long roleIds, List<Long> resourceIds) {
        checkParas(roleIds, resourceIds);
        if (null == resourceIds || resourceIds.isEmpty()) {
            throw new InvalidArgumentException("resourceIds不能为空");
        }
        return resourceMapper.deleteRoleResource(roleIds, resourceIds);
    }

    private void checkParas(Long roleId, List<Long> resourceIds) {
        if (roleId <= 0 || resourceIds.isEmpty()) {
            throw new InvalidArgumentException("角色或者资源不能为空");
        }
        Role role = roleService.get(roleId);
        if (role == null) {
            throw new InvalidArgumentException("角色不存在(id:" + roleId + ")");
        }
        for (Long s : resourceIds) {
            Resource resource = resourceMapper.get(s);
            if (resource == null) {
                throw new InvalidArgumentException("资源不存在(id:" + s + ")");
            }
        }
    }
}